﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MyBank
{

    public delegate void BigMoneyHandler(double amount);
    public partial class Form1 : Form
    {
        Bank b1 = new Bank();
        public double Mymoney = 100000;
        public int lorb = 0;
        public double temp_money=0;

        public Form1()
        {
            InitializeComponent();
            lend_.Enabled = false;
            bw.Enabled = false;
            button3.Enabled = false;
            left_money.Visible = false;
            money.Visible = false;
            tips.Visible = false;
            trans_money.Visible = false;
            comfirm.Visible = false;
            CreditAccount c2 = new CreditAccount();
            c2.name = "psh";
            c2.password = "12345";
            c2.creditLimit = 10000;
            b1.CreditAccounts.Append(c2);
            CreditAccount c1 = new CreditAccount();
            c1.temp = 12000;
            // 使用ATM
            ATM atm = new ATM();
         
            c1.Transfer();
         
            // 可能抛出自定义异常
        }
        public static class GlobalVariables
        {
            public static string Name = "psh";
            public static string password = "123";
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void login_button_Click(object sender, EventArgs e)
        {
            

            // 存储用户名
            if ((GlobalVariables.Name==user_name.Text) && (GlobalVariables.password == password.Text))
                {
                user_name.Visible = false;
                password.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                money.Visible = true;
                left_money.Visible= true;
                lend_.Enabled = true;
                bw.Enabled = true;
                button3.Enabled = true;
                login_button.Enabled = false;
            }
            else
            {
                MessageBox.Show("账户或密码错误");
            }
            user_name.Text = "";
            password.Text = "";
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
           
        }

        private void lend__Click(object sender, EventArgs e)
        {
            lorb = 1;
            tips.Visible = true;
            trans_money.Visible = true;
            comfirm.Visible = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            user_name.Visible = true;
            password.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            money.Visible = false;
            left_money.Visible = false;
            lend_.Enabled = false;
            bw.Enabled = false;
            button3.Enabled = false;
            login_button.Enabled = true;
        }

        private void bw_Click(object sender, EventArgs e)
        {
            lorb = 0;
            tips.Visible = true;
            trans_money.Visible = true;
            comfirm.Visible = true;
        }

        private void comfirm_Click(object sender, EventArgs e)
        {
            temp_money = double.Parse(trans_money.Text);
            if (lorb == 0)
            {
                if(temp_money>10000)
                    MessageBox.Show("取款超出10000");
                Mymoney = Mymoney - temp_money;
                money.Text = Mymoney.ToString();
            }
            if (lorb == 1)
            {
             
                   
                Mymoney = Mymoney + temp_money;
                money.Text = Mymoney.ToString();
            }
            tips.Visible = false;
            trans_money.Visible = false;
            comfirm.Visible = true;
            trans_money.Text = "";
            comfirm.Visible = false;
        }

        private void trans_money_TextChanged(object sender, EventArgs e)
        {

        }
    }
    public class Bank
    {
        // 字段
        private string name;
        public List<CreditAccount> CreditAccounts = new List<CreditAccount>();
        // 属性
        
       
        public bool FindAccount(string usr,string password) { 
            foreach ( CreditAccount account in CreditAccounts){
                if (account.name == usr)
                    if (account.password == password)
                        return true;
               
            }
            return false;
        }
        // 方法
        public void OpenAccount() { }
    }
    public static class GlobalInstance
    {
        public static CreditAccount c2 = new CreditAccount();
    }
    // 账户
    public abstract class Account
    {
        // 字段
        
        protected double balance;

        // 属性
        public double Balance { get; set; }

        // 方法
        public abstract void lend();
        public abstract void borrow();
    }

    // 信用账户
    public class CreditAccount : Account
    {
        // 新增字段
        public string name;
        public string password;
        public double creditLimit;
        public double temp;
        // 重写方法
        public override void lend()
        {
            creditLimit += temp;
        }
        public override void borrow()
        {
            creditLimit -= temp;
        }
        public event BigMoneyHandler BigMoneyEvent;
        public void Transfer()
        {
            // 转账逻辑

            if (temp > 10000)
            {
                BigMoneyEvent?.Invoke(temp);
            }

        }
    }

    // ATM
    public class ATM
    {
        // 方法
        public void Subscribe(CreditAccount creditAccount)
        {
            creditAccount.BigMoneyEvent += above;
            creditAccount.BigMoneyEvent += bad_money;
        }

        public void WithdrawCash() { }
        public void above(double amount)
        {
            MessageBox.Show("超出10000");


        }
        public void bad_money(double amount)
        {
            if (RandomNumber() > 0.3)
            {
                MessageBox.Show("出现坏钞");
            }

        }
        private static Random random = new Random();

        private double RandomNumber()
        {
            return 0.4;
        }
    }
    // 大额取款事件参数
    public class BigMoneyArgs
    {
        public CreditAccount Account { get; set; }
        public double Amount { get; set; }
    }

    // 大额取款事件
    public class BadCashException : Exception
    {
        public BadCashException() { }
        public BadCashException(string message) : base(message) { }
    }


}


